//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "OvernightLeg.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/CashFlow.h>
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Indexes/OvernightIndex.h>
#include <gen/QL/Times/DayCounter.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::COvernightLeg::COvernightLeg (Cephei::QL::Times::ISchedule^ schedule, Cephei::QL::Indexes::IOvernightIndex^ overnightIndex) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CSchedule^ _Cschedule;
    COvernightIndex^ _CovernightIndex;
    try
    {
#ifdef HANDLE
        _phOvernightLeg = NULL;
#endif
        _Cschedule = safe_cast<CSchedule^> (schedule);
        _Cschedule->Lock();
        QuantLib::Schedule& _schedule = static_cast<QuantLib::Schedule&> (_Cschedule->GetReference ()); 
        _CovernightIndex = safe_cast<COvernightIndex^> (overnightIndex);
        _CovernightIndex->Lock();
        boost::shared_ptr<QuantLib::OvernightIndex>& _overnightIndex = static_cast<boost::shared_ptr<QuantLib::OvernightIndex>&> (_CovernightIndex->GetShared ()); 
        _ppOvernightLeg = new boost::shared_ptr<QuantLib::OvernightLeg> (new QuantLib::OvernightLeg ( _schedule,  _overnightIndex ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cschedule != nullptr) _Cschedule->Unlock();
        if (_CovernightIndex != nullptr) _CovernightIndex->Unlock();
    }
}
Cephei::QL::Cashflows::COvernightLeg::COvernightLeg (boost::shared_ptr<QuantLib::OvernightLeg>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOvernightLeg = NULL;
#endif
	_ppOvernightLeg = &childNative;
    
}
Cephei::QL::Cashflows::COvernightLeg::COvernightLeg (QuantLib::OvernightLeg& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phOvernightLeg = NULL;
#endif
	_ppOvernightLeg = new boost::shared_ptr<QuantLib::OvernightLeg> (&childNative);
    
    _OvernightLegOwner = owner;
    
}

Cephei::QL::Cashflows::COvernightLeg::COvernightLeg (COvernightLeg^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOvernightLeg = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOvernightLeg = new boost::shared_ptr<QuantLib::OvernightLeg> (copy->GetShared());
        
    }
}
Cephei::QL::Cashflows::COvernightLeg::COvernightLeg (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOvernightLeg = NULL;
#endif
	if (!t->IsSubclassOf(COvernightLeg::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::COvernightLeg::COvernightLeg (QuantLib::Handle<QuantLib::OvernightLeg>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phOvernightLeg = &childNative;
	_ppOvernightLeg = &static_cast<boost::shared_ptr<QuantLib::OvernightLeg>>(childNative.currentLink());
    
    _OvernightLegOwner = owner;
}
Cephei::QL::Cashflows::COvernightLeg::COvernightLeg (QuantLib::Handle<QuantLib::OvernightLeg> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phOvernightLeg = &childNative;
	_ppOvernightLeg = &static_cast<boost::shared_ptr<QuantLib::OvernightLeg>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::COvernightLeg::COvernightLeg (QuantLib::OvernightLeg childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOvernightLeg = NULL;
#endif
	_ppOvernightLeg = new boost::shared_ptr<QuantLib::OvernightLeg> (new QuantLib::OvernightLeg (childNative));
    
}
#endif

Cephei::QL::Cashflows::COvernightLeg::~COvernightLeg ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppOvernightLeg != NULL)
    {
	    delete _ppOvernightLeg;
        _ppOvernightLeg = NULL;
    }
}
Cephei::QL::Cashflows::COvernightLeg::!COvernightLeg ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppOvernightLeg != NULL)
    {
	    delete _ppOvernightLeg;
    }
}
QuantLib::OvernightLeg& Cephei::QL::Cashflows::COvernightLeg::GetReference ()
{
    if (_ppOvernightLeg == NULL) throw REFNEW NativeNullException ();
	return **_ppOvernightLeg;
}
boost::shared_ptr<QuantLib::OvernightLeg>& Cephei::QL::Cashflows::COvernightLeg::GetShared ()
{
    if (_ppOvernightLeg == NULL) throw REFNEW NativeNullException ();
	return *_ppOvernightLeg;
}
QuantLib::OvernightLeg* Cephei::QL::Cashflows::COvernightLeg::GetPointer ()
{
    if (_ppOvernightLeg == NULL) throw REFNEW NativeNullException ();
	return &**_ppOvernightLeg;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OvernightLeg>& Cephei::QL::Cashflows::COvernightLeg::GetHandle ()
{
	if (_phOvernightLeg == NULL)
	{
		_phOvernightLeg = new Handle<QuantLib::OvernightLeg> (*_ppOvernightLeg);
	}
	return *_phOvernightLeg;
}
#endif
bool Cephei::QL::Cashflows::COvernightLeg::HasNative () 
{
	return (_ppOvernightLeg != NULL);
}

Cephei::QL::Cashflows::IOvernightLeg^ Cephei::QL::Cashflows::COvernightLeg::WithGearings (Cephei::Core::IVector<Double>^ gearings)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        gearings->Lock();
        INativeVector<Double>^ _NCIgearings = gearings->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCgearings = safe_cast<CDoubleVector^>(_NCIgearings);
        std::vector<QuantLib::Real>& _gearings = static_cast<std::vector<QuantLib::Real>&> (_NCgearings->GetReference ());
    	QuantLib::OvernightLeg& _rv = (QuantLib::OvernightLeg&)(*_ppOvernightLeg)->withGearings ( _gearings );   
        Cephei::QL::Cashflows::COvernightLeg^ _nrv = REFNEW Cephei::QL::Cashflows::COvernightLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (gearings != nullptr) gearings->Unlock(); //NO2  
    }
}
Cephei::QL::Cashflows::IOvernightLeg^ Cephei::QL::Cashflows::COvernightLeg::WithNotionals (Cephei::Core::IVector<Double>^ notionals)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        notionals->Lock();
        INativeVector<Double>^ _NCInotionals = notionals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCnotionals = safe_cast<CDoubleVector^>(_NCInotionals);
        std::vector<QuantLib::Real>& _notionals = static_cast<std::vector<QuantLib::Real>&> (_NCnotionals->GetReference ());
    	QuantLib::OvernightLeg& _rv = (QuantLib::OvernightLeg&)(*_ppOvernightLeg)->withNotionals ( _notionals );   
        Cephei::QL::Cashflows::COvernightLeg^ _nrv = REFNEW Cephei::QL::Cashflows::COvernightLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (notionals != nullptr) notionals->Unlock(); //NO2  
    }
}
Cephei::QL::Cashflows::IOvernightLeg^ Cephei::QL::Cashflows::COvernightLeg::WithPaymentAdjustment (QL::Times::BusinessDayConventionEnum convention)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::BusinessDayConvention _convention = (QuantLib::BusinessDayConvention)convention ;
    	QuantLib::OvernightLeg& _rv = (QuantLib::OvernightLeg&)(*_ppOvernightLeg)->withPaymentAdjustment ( _convention );   
        Cephei::QL::Cashflows::COvernightLeg^ _nrv = REFNEW Cephei::QL::Cashflows::COvernightLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IOvernightLeg^ Cephei::QL::Cashflows::COvernightLeg::WithPaymentDayCounter (Cephei::QL::Times::IDayCounter^ dc)
{
    CDayCounter^ _Cdc;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
    	QuantLib::OvernightLeg& _rv = (QuantLib::OvernightLeg&)(*_ppOvernightLeg)->withPaymentDayCounter ( _dc );   
        Cephei::QL::Cashflows::COvernightLeg^ _nrv = REFNEW Cephei::QL::Cashflows::COvernightLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Cashflows::IOvernightLeg^ Cephei::QL::Cashflows::COvernightLeg::WithSpreads (Cephei::Core::IVector<Double>^ spreads)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        spreads->Lock();
        INativeVector<Double>^ _NCIspreads = spreads->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCspreads = safe_cast<CDoubleVector^>(_NCIspreads);
        std::vector<QuantLib::Spread>& _spreads = static_cast<std::vector<QuantLib::Spread>&> (_NCspreads->GetReference ());
    	QuantLib::OvernightLeg& _rv = (QuantLib::OvernightLeg&)(*_ppOvernightLeg)->withSpreads ( _spreads );   
        Cephei::QL::Cashflows::COvernightLeg^ _nrv = REFNEW Cephei::QL::Cashflows::COvernightLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (spreads != nullptr) spreads->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IOvernightLeg^ Cephei::QL::Cashflows::COvernightLeg_Factory::Create (Cephei::QL::Times::ISchedule^ schedule, Cephei::QL::Indexes::IOvernightIndex^ overnightIndex)
{
    return REFNEW COvernightLeg ( schedule,  overnightIndex);
}
